<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 面包屑下面的文字 -->
    <el-button-group style="margin: 15px 0">
      <el-button type="primary" @click="goodsList">全部商品</el-button>
      <el-button type="primary" @click="yishangjia">已上架</el-button>
      <el-button type="primary" @click="yixiajia">已下架</el-button>
    </el-button-group>

    <!-- 搜索行 -->
    <!--
        goodsStatus: null,
        shopName: "",
        startTime: "",
        endTime: "", -->
    <el-form :inline="true" :model="queryInfo" class="demo-form-inline">
      <el-form-item label="商品名称:">
        <el-input
          v-model="queryInfo.goodsName"
          placeholder="商品名称"
          @clear="goodsList"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="所属门店:">
        <el-input
          clearable
          @clear="goodsList"
          v-model="queryInfo.shopName"
          placeholder="所属门店"
        ></el-input>
      </el-form-item>

      <el-form-item label="发布日期:">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="datevalue"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="goodsList">搜索</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="goodslist" style="width: 100%">
      <el-table-column prop="goodsId" label="商品ID" width="150">
      </el-table-column>
      <el-table-column prop="goodsName" label="商品" width="150">
      </el-table-column>
      <el-table-column prop="goodsShopName" label="所属门店" width="150">
      </el-table-column>
      <el-table-column prop="goodsSalesVolume" label="销量" width="150">
      </el-table-column>
      <el-table-column prop="goodsPresentPrice" label="预估价" width="150">
      </el-table-column>
      <el-table-column prop="addTime" label="发布时间" width="150">
      </el-table-column>
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.goodsStatus == 1"
            >上架中</el-button
          >
          <el-button type="text" v-if="scope.row.goodsStatus == 0"
            >下架中</el-button
          >
        </template>
      </el-table-column>

      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" @click="upOrDown(scope.row)" v-if="scope.row.goodsStatus == 1"
            >下架</el-button
          >
          <el-button type="text" @click="upOrDown(scope.row)" v-if="scope.row.goodsStatus == 0"
            >上架</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :total="newtotal"
    >
    </el-pagination>   
  </div>
</template>
  
  <script scoped>
export default {
  data() {
    return {
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
        goodsName: "",
        shopName: "",
        startTime: "",
        endTime: "",
        goodsStatus:null,
      },
      queryInfo1: {
        pageNum: 1,
        pageSize: 5,
        goodsName: "",
        shopName: "",
        startTime: "",
        endTime: "",
        goodsStatus:1,
      },
      queryInfo0: {
        pageNum: 1,
        pageSize: 5,
        goodsName: "",
        shopName: "",
        startTime: "",
        endTime: "",
        goodsStatus:0,
      },
      
      newtotal: 1,
      datevalue: "",
      goodslist: [],
    };
  },
  methods: {
    upOrDown(row){
      this.$http.post('goods/modifyShopGoods',
        {
          goodsId:row.goodsId,
          goodsStatus:(row.goodsStatus==1)? 0:1
        }
      )
      .then(res=>{
        if (res.data.status == 200) {
            this.$message.success(res.data.message);
            this.goodsList();
          }else{
            this.$message.error(res.data.message)
          }
      })
      .catch(error=>{
        this.$message.error('请求错误')
      })
    },
    //已上架
    yishangjia(){
      this.$http
        .get("goods/querySearchShopGoodsList", {
          params: this.queryInfo1,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.goodslist = res.data.data.list;
            this.newtotal = res.data.data.total;
          } else {
            this.$message.error("请求错误");
          }
        })
        .catch((error) => {
          console.log(error);
        });
      

    },
    yixiajia(){
      this.$http
        .get("goods/querySearchShopGoodsList", {
          params: this.queryInfo0,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.goodslist = res.data.data.list;
            this.newtotal = res.data.data.total;
          } else {
            this.$message.error("请求错误");
          }
        })
        .catch((error) => {
          console.log(error);
        });
      
    },
    goodsList() {
      this.$http
        .get("goods/querySearchShopGoodsList", {
          params: this.queryInfo
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.goodslist = res.data.data.list;
            this.newtotal = res.data.data.total;
          } else {
            this.$message.error("请求错误");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.queryInfo.pageNum = 1;
      this.goodsList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val;
      this.goodsList();
    },
  },

  mounted() {
    this.goodsList();
  },
  watch: {
    datevalue() {
      this.queryInfo.startTime = this.datevalue[0];
      this.queryInfo.endTime = this.datevalue[1];
    },
  },
};
</script>
  
  <style>
</style>